package com.tekj.cxqcb.view.Module.activity;

import android.content.Intent;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.tekj.cxqcb.R;
import com.tekj.cxqcb.operation.BusinessDao;
import com.tekj.cxqcb.operation.resultBean.DoGetCaristForBrandBean;
import com.tekj.cxqcb.operation.resultBean.selectAllBrandBean;
import com.tekj.cxqcb.view.Module.adapter.BrandAdapter;
import com.tekj.cxqcb.view.Module.adapter.VehicleTypeadapter;
import com.tekj.cxqcb.view.Module.decoration.SpacesItemDecoration;

import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import common.base.activity.BaseActivity;
import common.base.operationHelper.Bean.BindingViewBean;
import common.tool.pinying.PinyinComparatorBrand2;
import common.tool.pinying.PinyinUtils;
import common.tool.pinying.TitleItemDecorationBrand2;
import common.widget.WaveSideBar;

/**
 * 品牌选择
 */
public class BrandActivity extends BaseActivity {
    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.tv_l)
    TextView tvL;
    @BindView(R.id.ll_back)
    LinearLayout llBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_r)
    ImageView ivR;
    @BindView(R.id.tv_r)
    TextView tvR;
    @BindView(R.id.ll_right)
    LinearLayout llRight;
    @BindView(R.id.rl_title_bg)
    RelativeLayout rlTitleBg;
    @BindView(R.id.rv_main)
    RecyclerView rvMain;
    @BindView(R.id.sideBar)
    WaveSideBar sideBar;

    private LinearLayoutManager manager;
    private PinyinComparatorBrand2 mComparator;
    private BrandAdapter adapter;
    private List<selectAllBrandBean.DataBean.BrandListBean> mDateList;
    private TitleItemDecorationBrand2 mDecoration;

    private VehicleTypeadapter typeadapter;

    private BusinessDao mod5Dao;

    @Override
    public void init() {
        tvTitle.setText("品牌选择");
        mod5Dao = new BusinessDao(activity, this);
        mComparator = new PinyinComparatorBrand2();
        mod5Dao.DoGetCarBrandAndModel();
    }

    @Override
    public int setLayout() {
        return R.layout.activity_brand;
    }

    @Override
    public void initLoadStart() {

    }

    @Override
    public void initLoadEnd() {

    }

    @Override
    public void initBindingView(Object mBean) {
        BindingViewBean bindingViewBean = (BindingViewBean) mBean;
        switch (bindingViewBean.getmEnumStatus()) {
            case 车品牌:
                selectAllBrandBean modelBean = (selectAllBrandBean) bindingViewBean.getBean();
                if ("0000000".equals(modelBean.getCode())) {
                    mDateList = filledData(modelBean.getData().getBrandList());
                    initSortAdapter();
                }
                break;
            case 车系:
                DoGetCaristForBrandBean doGetCaristForBrandBean = (DoGetCaristForBrandBean) bindingViewBean.getBean();
                if (doGetCaristForBrandBean.getCode().equals("0000000")) {
                    typeadapter.setNewData(doGetCaristForBrandBean.getData().getList());
//                    mDecoration
                    rvMain.removeItemDecoration(mDecoration);
                    rvMain.setAdapter(typeadapter);
                    sideBar.setVisibility(View.GONE);
                }
                break;
        }
    }


    @OnClick(R.id.ll_back)
    public void onViewClicked() {
        if (sideBar.getVisibility() == View.GONE) {
            sideBar.setVisibility(View.VISIBLE);
            rvMain.addItemDecoration(mDecoration);
            rvMain.setAdapter(adapter);
        } else
            finish();
    }

    /**
     * 为RecyclerView填充数据
     *
     * @param date
     * @return
     */
    public List<selectAllBrandBean.DataBean.BrandListBean> filledData(List<selectAllBrandBean.DataBean.BrandListBean> date) {

        for (int i = 0; i < date.size(); i++) {
            //汉字转换成拼音
            String pinyin = PinyinUtils.getPingYin(date.get(i).getName());
            String sortString = pinyin.substring(0, 1).toUpperCase();

            // 正则表达式，判断首字母是否是英文字母
            if (sortString.matches("[A-Z]")) {
                date.get(i).setLetters(sortString.toUpperCase());
            } else {
                date.get(i).setLetters("#");
            }
        }
        return date;

    }


    boolean isf = true;
    DividerItemDecoration dividerItemDecoration;

    void initSortAdapter() {

        // 根据a-z进行排序源数据
        if (isf)
            Collections.sort(mDateList, mComparator);

        //RecyclerView设置manager
        manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        rvMain.setLayoutManager(manager);
        rvMain.setHasFixedSize(true);
        adapter = new BrandAdapter(mDateList);
        rvMain.setAdapter(adapter);

        if (isf) {
            mDecoration = new TitleItemDecorationBrand2(this, mDateList);
            dividerItemDecoration = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
            //如果add两个，那么按照先后顺序，依次渲染。
            rvMain.addItemDecoration(mDecoration);
            rvMain.addItemDecoration(new SpacesItemDecoration(1));

            //设置右侧SideBar触摸监听
            sideBar.setOnTouchLetterChangeListener(new WaveSideBar.OnTouchLetterChangeListener() {
                @Override
                public void onLetterChange(String letter) {
                    //该字母首次出现的位置
                    int position = adapter.getPositionForSection(letter.charAt(0));
                    if (position != -1) {
                        manager.scrollToPositionWithOffset(position, 0);
                    }
                }
            });
        } else {
            rvMain.removeItemDecoration(mDecoration);
//            rvMain.removeItemDecoration(dividerItemDecoration);

        }
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                mod5Dao.DoGetCaristForBrand(BrandActivity.this.adapter.getData().get(position).getId());
            }
        });

        isf = false;

        typeadapter = new VehicleTypeadapter(null);

        typeadapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent();
                intent.putExtra("brand", typeadapter.getData().get(position));
                setResult(RESULT_OK, intent);
                finish();
            }
        });
    }

}
